﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjetoJornada_Negocios.Fachada;
using Basicas;
using Basicas.Utils.excecoes;

namespace ProjetoJornada.Interno
{
    public partial class Matricula_Editar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string matriculaId = Request.QueryString["id"];
                Matricula matricula = Fachada.Instance.BuscarMatriculaPorId(Convert.ToInt32(matriculaId));
                this.txtCpf.Text = matricula.Aluno.Cpf;
                Turma turma = Fachada.Instance.ConsultarTurmaPorId(matricula.Turma.Id);
                this.txtTurma.Text = turma.Nome;
                this.txtCusto.Text = turma.PrecoAtual.ToString();
                this.txtValor.Text = matricula.ValorPago.ToString();

                List<Desconto> descs = Fachada.Instance.ConsultarTodsDescontos();
                this.ddlDesconto.Items.Add(new ListItem("-", null));
                this.ddlDesconto.SelectedIndex = 0;
                this.txtValorDesconto.Text = "0";
                this.txtValorFinal.Text = turma.PrecoAtual.ToString();  
                foreach (Desconto desconto in descs)
                {
                    this.ddlDesconto.Items.Add(new ListItem(desconto.Motivo, desconto.Id.ToString()));
                    if (matricula.Desconto != null)
                    {
                        if (desconto.Id == matricula.Desconto.Id)
                        {
                            this.ddlDesconto.SelectedIndex = this.ddlDesconto.Items.Count - 1;
                            this.txtValorDesconto.Text = desconto.Valor.ToString();
                            this.txtValorFinal.Text = (turma.PrecoAtual - desconto.Valor).ToString();
                        }
                    }
                }

                Session["Matricula"] = matricula;
            }
        }

        protected void ibtnEditar_Click(object sender, ImageClickEventArgs e)
        {
            Matricula matricula = Session["Matricula"] as Matricula;
            matricula.ValorPago = Convert.ToDouble(this.txtValor.Text);
            if (matricula.Desconto == null)
            {
                matricula.Desconto = new Desconto();
            }
            matricula.Desconto.Id = Convert.ToInt32(this.ddlDesconto.SelectedValue);
            matricula.FormaPagamento = new FormaPagamento();

            double valorFinal = Convert.ToDouble(this.txtValorFinal.Text);
            double valorPago = Convert.ToDouble(this.txtValor.Text);

            if (valorPago >= valorFinal)
            {
                matricula.StatusPagamento = true;
            }
            else 
            {
                matricula.StatusPagamento = false;
            }
            
            Fachada.Instance.EditarMatricula(matricula);

            Response.Redirect(@"\Interno\Matricula_ListarPorTurma.aspx");
        }

        protected void ddlDesconto_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedId = this.ddlDesconto.SelectedValue;
            Desconto desconto = Fachada.Instance.ConsultarDescontoPorId(Convert.ToInt32(selectedId));
            this.txtValorDesconto.Text = desconto.Valor.ToString();

            double custo = Convert.ToDouble(this.txtCusto.Text);

            this.txtValorFinal.Text = (custo - desconto.Valor).ToString();
        }
    }
}
